package service.impl;

import dao.TeacherDao;
import dao.impl.TeacherDaoImpl;
import domain.Course;
import domain.PageBean;
import domain.Student;
import domain.Teacher;
import service.TeacherService;

import java.util.List;
import java.util.Map;

public class TeacherServiceImpl implements TeacherService {
    private TeacherDao dao = new TeacherDaoImpl();

    @Override
    public Teacher login(Teacher teacher) {
        return dao.findTeacheridAndPassword(teacher.getT_id(), teacher.getT_password());
    }

    @Override
    public PageBean<Teacher> findTeacherByPage(String _currentPage, String _rows, Map<String, String[]> condition) {

        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        //创建新的PageBean对象
        PageBean<Teacher> pb = new PageBean<Teacher>();

        //设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //调用dao查询总记录数
        int totalCount = dao.findTotalCount(condition);
        pb.setTotalCount(totalCount);

        //调用dao查询List集合
        //计算开始记录的索引
        int start = (currentPage - 1) * rows;
        List<Teacher> list = dao.findByPage(start, rows, condition);
        pb.setList(list);

        //计算总页码
        int totalPage = (totalCount % rows) == 0 ? totalCount / rows : (totalCount / rows) + 1;
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public List<Teacher> findAll() {
        return dao.findAll();
    }

    @Override
    public List<Course> findMySelfOptionalCourse(String T_id) {
        return dao.findMySelfOptionalCourse(T_id);
    }

    @Override
    public Course findOptionalCourseByCourseId(String cid) {
        return dao.findOptionalCourseByCourseId(cid);
    }

    @Override
    public void updateCourseInfo(Course updateCourse) {
        dao.updateCourseInfo(updateCourse);
    }

    @Override
    public void deleteCourseById(String cid) {
        dao.deleteCourseById(cid);
    }

    @Override
    public void updatePassword(String teacherid, String newpassword) {
        dao.updatePassword(teacherid, newpassword);
    }

    @Override
    public Teacher findTeacherById(Teacher teacher) {
        return dao.findTeacherById(teacher.getT_id());
    }

    @Override
    public void addTeacherAllInfo(Teacher updateTeacher) {
        dao.addTeacherAllInfo(updateTeacher);
    }

    @Override
    public void deleteTeacherById(String teacherid) {
        dao.deleteTeacherById(teacherid);
    }

    @Override
    public void updateInfo(Teacher updateTeacher) {
        dao.updateInfo(updateTeacher);
    }
}
